/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.shared.XML;

import avl.sv.shared.SlideReference;
import avl.sv.shared.SlideSet;
import avl.sv.shared.SlideSets;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author benbryan
 */
public class SlideSetsXML_Parser {

    private SlideSets slideSets;
    private SlideSet slideSet;
                     
    public SlideSets parse(String s) {
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance( );
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            InputStream is = new ByteArrayInputStream(s.getBytes());
            Document doc = docBuilder.parse(is);             
            return parse(doc);
        } catch (SAXException | IOException | ParserConfigurationException ex) {
            Logger.getLogger(SlideSetsXML_Parser.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    private SlideSets parse(Document doc) {
        doc.getDocumentElement().normalize();
        slideSets = new SlideSets();
        slideSet = null;
        
        for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) {
                if ("slideSets".equalsIgnoreCase(n.getNodeName())) {
                parseSlideSets(n);
            }
        }
        
        return slideSets;
    }
    
    private void parseSlideSets(Node n) {
        for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
            if ("slideSet".equalsIgnoreCase(n.getNodeName())) {
                if (slideSet != null) {
                    slideSets.add(slideSet);
                }
                NamedNodeMap a = n.getAttributes();
                String slideSetName = NamedNodeMapFunc.getString(a,"name");
                slideSet = new SlideSet(slideSetName);
                parseSlides(n);
            }
        }
        if (slideSet != null) {
            slideSets.add(slideSet);
        }
    }

    private void parseSlides(Node n) {
        for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
            if ("slide".equalsIgnoreCase(n.getNodeName())) {
                NamedNodeMap a = n.getAttributes();
                String slideName = NamedNodeMapFunc.getString(a,"slideName");
                String slideSetName = NamedNodeMapFunc.getString(a,"slideSetName");
                String hashBase64 = NamedNodeMapFunc.getString(a,"hash");
                byte hash[] = DatatypeConverter.parseBase64Binary(hashBase64);
                SlideReference slideReference = new SlideReference(slideSetName, slideName, hash);
                slideSet.add(slideReference);
            }
        }
    }
           
}
